var form, $,areaData;

layui.use(['form','layer','upload','laydate','jquery'],function(){
    form = layui.form;
    $ = layui.jquery;
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        upload = layui.upload,
        laydate = layui.laydate;
    var xhrOnProgress=function(fun) {
        xhrOnProgress.onprogress = fun; //绑定监听
        //使用闭包实现监听绑
        return function() {
            //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
            var xhr = $.ajaxSettings.xhr();
            //判断监听函数是否为函数
            if (typeof xhrOnProgress.onprogress !== 'function')
                return xhr;
            //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
            if (xhrOnProgress.onprogress && xhr.upload) {
                xhr.upload.onprogress = xhrOnProgress.onprogress;
            }
            return xhr;
        }
    }
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
    }
    var param =  getUrlParam("id");
    if (param == null){
        param = 'self';
    }
    var j ;
    $.ajax({
        url: '../user.do',
        data: {
            'tran':'temp',
            'id':param
        },
        dataType: 'json',
        type: 'post',
        success:function (res) {

        }
    })
    $.ajax({
        url: '../user.do',
        data: {
            'tran':'select',
            'get':'info',
            'id':'self'
        },
        dataType: 'json',
        type: 'post',
        async: false,
        success: function(data) {
            if (data.permissions == '0') {
                $("#department").removeAttr("disabled");
                $("#department").removeClass("layui-disabled");
                $("#group").removeAttr("disabled");
                $("#group").removeClass("layui-disabled");
                $("#username").removeAttr("disabled");
                $("#username").removeClass("layui-disabled");
                $("#username").removeClass("layui-disabled");
            }else if(data.permissions == '1'){
                $("#group").removeAttr("disabled");
                $("#username").removeAttr("disabled");
            }else if(data.id == param || param == "self"){

            }else {
                $("#phone").attr("disabled","disabled");
                $("#passworddiv").hide();
                $("#submit").hide();
                $(".userFaceBtn").removeClass("userFaceBtn");
                $("#face").hide();
            }

        }
    });
    $.ajax({
        type : "post",
        url : "../department.do",
        data : {
            tran:'select',
            type:'self'
        },
        async:false,
        dataType : "json",
        success : function(f) {
            var tmp = '<option value="">--请选择部门--</option>';
            var d = f.data;
            $(".department").html(tmp);
            for ( var i in d) {
                tmp += '<option value="' + d[i].id +  '">' + d[i].departmentName + '</option>';
            }
            $(".department").html(tmp);
            form.render();
        }
    });

    form.on('select(department)', function(data) {
        var departmentId = data.value;
        $.ajax({
            type : "post",
            url : "../group.do",
            data : {
                tran:'select',
                type:'id',
                departmentId:departmentId
            },
            dataType : "json",
            success : function(f) {
                var tmp = '<option value="">--请选择组别--</option>';
                var d = f.data;
                $(".group").html(tmp);
                for ( var i in d) {
                    tmp += '<option value="' + d[i].id +  '">' + d[i].group + '</option>';
                }
                $(".group").html(tmp);
                form.render();
            }
        });
    });
    $.ajax({
        url: '../user.do',
        data: {
            'tran':'select',
            'get':'info',
            'id':param
        },
        dataType: 'json',
        type: 'post',
        success: function(data) {
            console.log(data)
            $("#id").val(data.id);
            $("#username").val(data.username);
            $("#department").val(data.department);
            $("#departmenthide").val(data.departmentId);
            $("#group").val(data.group);
            $("#grouphide").val(data.groupId);
            if (data.sex == 0){
                $("#man").attr("checked","checked");
            }else if (data.sex == 1){
                $("#id").attr("checked","checked");
            }else if (data.sex == 2){
                $("#secrecy").attr("checked","checked");
            }
            form.render();
            $("#phone").val(data.phone);
            $("#userFace").attr("src","../user.do?tran=face&id="+param);
        }
    });
    //上传头像
    upload.render({
        elem: '.userFaceBtn',
        url: "../upface.do",
        method : "post",
        data:{
            tran:'face'
        },
        xhr:xhrOnProgress,
        progress:function(value){//上传进度回调 value进度值

        },
        dataType: 'json',
        before:function(obj){
            j = layer.load("头像上传中，可能需要一段时间，请稍后......",0);
            obj.preview(function(index, file, result){
                $('#userFace').attr('src', result); //图片链接（base64）
            });
        },
        done: function(res, index, upload){
            if(res.code == '1'){
                layer.close(j);
                return layer.msg(res.msg);
            }else {
                layer.close(j);
                return layer.msg(res.msg);
            }
        },
        error:function () {
            layer.close(j);
        }
    });

    //添加验证规则
    form.verify({
        userBirthday : function(value){
            if(!/^(\d{4})[\u4e00-\u9fa5]|[-\/](\d{1}|0\d{1}|1[0-2])([\u4e00-\u9fa5]|[-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/.test(value)){
                return "出生日期格式不正确！";
            }
        }
    })
    //选择出生日期
    laydate.render({
        elem: '.userBirthday',
        format: 'yyyy年MM月dd日',
        trigger: 'click'
    });

    //提交个人资料
    form.on("submit(changeUser)",function(data){
        var username = $("#username").val();
        var department;
        var group ;
        var phone = $("#phone").val();
        var password =  $("#password").val();
        if ($(".department").val() == "" ||$(".department").val() == ""){
            department  =   $("#departmenthide").val();
        }else {
            department =$(".department").val()
        }
        if ($(".group").val() == "" ||$(".group").val() == ""){
            group  =   $("#grouphide").val();
        }else {
            group =$(".group").val()
        }
        $.ajax({
            url: '../user.do',
            data: {
                'tran':'updata',
                'username':username,
                'department':department,
                'group':group,
                'phone':phone,
                'password':password,
                'id':$("#id").val()
            },
            dataType: 'json',
            type: 'post',
            success: function(data) {

            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })


})